package gtgold;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Party configuration screen
 * 
 * @author jen, jo, joeoh
 */
public class ConfigScreen extends JPanel
{
	private static final long serialVersionUID = 1L;
	private JTextField[] names;
	private JComboBox occupation;
	private JButton startButton;
	private String occImgName = "src/gtgold/img/BANKER.jpg";
	private JLabel occImg;
	private JLabel occDes;

	/**
	 * Constructor to create the panel.
	 */
	public ConfigScreen()
	{
		setPreferredSize(OregonTrail.VGA);
		setLayout(new GridLayout(1, 1));

		// Main BG JPanel
		bgImagePanel mainPanel = new bgImagePanel("src/gtgold/img/configBg.jpg");
		add(mainPanel, BorderLayout.CENTER);
		mainPanel.setLayout(new GridLayout(1, 1));

		// Secondary JPanel
		JPanel splitLayout = new JPanel();
		splitLayout.setLayout(new GridLayout(1, 2));

		// Attributes JPanel
		JPanel config = new JPanel();
		config.setLayout(new GridLayout(10, 1));
		for (int i = 0; i < 2; i++)
		{
			config.add(new JLabel());
		}
		names = new JTextField[5];
		JPanel[] namePanels = new JPanel[names.length];
		for (int i = 0; i < namePanels.length; i++)
		{
			namePanels[i] = new JPanel();
			namePanels[i].setOpaque(false);
			if (i == 0)
				namePanels[i].add(new JLabel("     Leader name: "));
			else
				namePanels[i].add(new JLabel("Party member " + (i + 1) + ": "));
			names[i] = new JTextField(15);
			namePanels[i].add(names[i]);
			config.add(namePanels[i]);
		}

		names[0].setText("You");

		JPanel _occupation = new JPanel();
		_occupation.setOpaque(false);
		occupation = new JComboBox(Occupation.values());
		occupation.addActionListener(new ComboBoxListener());
		_occupation.add(new JLabel("Leader's Occupation"));
		_occupation.add(occupation);
		config.add(_occupation);
		config.add(new JLabel());

		startButton = new JButton("Hit the Trail!");
		startButton.addActionListener(new ButtonListener());
		config.add(startButton);
		JPanel occInfo = new JPanel();
		occInfo.setOpaque(false);
		occInfo.setLayout(new BorderLayout());
		occImg = new JLabel(new ImageIcon(occImgName));
		occImg.setOpaque(false);
		occInfo.add(occImg, BorderLayout.CENTER);

		occDes = new JLabel();
		occDes.setOpaque(false);
		updateSelectedOccupationDescription();
		JPanel spacing = new JPanel();
		spacing.setOpaque(false);
		spacing.setLayout(new GridLayout(1, 3));
		spacing.add(new JLabel());
		spacing.add(occDes);
		spacing.add(new JLabel());

		// Adding all JPanels together
		occInfo.add(spacing, BorderLayout.SOUTH);
		splitLayout.add(occInfo);
		config.setOpaque(false);
		splitLayout.add(config);
		splitLayout.setOpaque(false);
		mainPanel.add(splitLayout);
		add(mainPanel);
	}

	private class ButtonListener implements ActionListener
	{
		public void actionPerformed(ActionEvent ae)
		{
			if (ae.getSource() == startButton)
			{
				// System.out.println("Set up the game objects!");

				List<String> nameList = new ArrayList<String>();
				for (JTextField e : names)
					nameList.add(e.getText());

				try
				{
					@SuppressWarnings("unused")
					GameManager theGame = new GameManager(
							(Occupation) occupation.getSelectedItem(), nameList);

				} catch (IllegalArgumentException e)
				{
					System.out.println("Error! " + e.getMessage());
				}
			}

		}
	}

	private class ComboBoxListener implements ActionListener
	{
		public void actionPerformed(ActionEvent ae)
		{
			occImgName = "src/gtgold/img/" + occupation.getSelectedItem()
					+ ".jpg";
			occImg.setIcon(new ImageIcon(occImgName));
			updateSelectedOccupationDescription();
		}
	}

	private void updateSelectedOccupationDescription()
	{
		Occupation o = (Occupation) occupation.getSelectedItem();
		String set = "<html>";
		set += "&nbsp;&nbsp;Starting Money: $";
		set += (int) o.startMoney + "<br>";
		set += "&nbsp;&nbsp;Score Multiplier: ";
		set += o.scoreMultiplier + "x<br>";

		// EDIT occupation bios in the Occupation.java class!
		set += "&nbsp;&nbsp;" + o.bio;
		set += "<br>&nbsp;</html>";

		occDes.setText(set);
	}

}